package com.yc.sole.system.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.common.enums.CommonState;
import com.yc.sole.system.dto.MenuDto;
import com.yc.sole.system.entity.Menu;
import com.yc.sole.system.mapper.MenuMapper;
import com.yc.sole.system.request.MenuQueryReq;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2023-03-26
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class MenuRepository extends ServiceImpl<MenuMapper, Menu> {

    /**
     * 新增
     *
     * @param menuDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public Long saveMenu(MenuDto menuDto) {
        Menu entity = new Menu();
        BeanUtil.copyProperties(menuDto, entity);
        this.save(entity);
        return entity.getId();
    }

    /**
     * 批量新增
     *
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveMenus(Collection<MenuDto> menuDtos) {
        List<Menu> menuList = BeanUtil.copyToList(menuDtos, Menu.class);
        this.saveBatch(menuList);

    }

    /**
     * 通过Id修改
     *
     * @param menuDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateMenuById(MenuDto menuDto) {
        Menu entity = new Menu();
        BeanUtil.copyProperties(menuDto, entity);
        this.updateById(entity);
    }

    /**
     * 通过id删除
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteMenuByIds(Collection<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public MenuDto selectMenuById(Long id) {
        return BeanUtil.copyProperties(this.getById(id), MenuDto.class);
    }

    /**
     * 根据id获取
     *
     * @param menuIds
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public List<MenuDto> selectEnableMenuByIds(Set<Long> menuIds) {
        LambdaQueryWrapper<Menu> wrapper = Wrappers.<Menu>lambdaQuery()
                .in(Menu::getId, menuIds)
                .eq(Menu::getState, CommonState.ENABLE)
                .orderByAsc(Menu::getSort);
        List<MenuDto> menuDtoList = BeanUtil.copyToList(this.list(wrapper), MenuDto.class);
        Set<Long> parentIds = menuDtoList.stream().map(MenuDto::getParentId).collect(Collectors.toSet());
        if (CollUtil.isNotEmpty(parentIds)) {
            menuDtoList.addAll(this.selectEnableMenuByIds(parentIds));
        }
        return menuDtoList;
    }

    /**
     * 获取全部
     *
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public List<MenuDto> selectAllEnableMenus() {
        LambdaQueryWrapper<Menu> wrapper = Wrappers.<Menu>lambdaQuery()
                .eq(Menu::getState, CommonState.ENABLE)
                .orderByAsc(Menu::getSort);
        ;
        return BeanUtil.copyToList(this.list(wrapper), MenuDto.class);
    }

    /**
     * 查詢菜單
     *
     * @param menuQueryReq
     * @return
     */
    public List<MenuDto> selectMenuList(MenuQueryReq menuQueryReq) {
        LambdaQueryWrapper<Menu> wrapper = Wrappers.<Menu>lambdaQuery()
                .like(CharSequenceUtil.isNotBlank(menuQueryReq.getMenuName()), Menu::getMenuName, menuQueryReq.getMenuName())
                .eq(Objects.nonNull(menuQueryReq.getMenuType()), Menu::getMenuType, menuQueryReq.getMenuType())
                .eq(Objects.nonNull(menuQueryReq.getState()), Menu::getState, menuQueryReq.getState())
                .orderByAsc(Menu::getSort);
        return BeanUtil.copyToList(this.list(wrapper), MenuDto.class);
    }

    /**
     * 根据父级id查询
     *
     * @param parentId
     * @return
     */
    public List<MenuDto> selectByParentId(Long parentId) {
        LambdaQueryWrapper<Menu> wrapper = Wrappers.<Menu>lambdaQuery().eq(Menu::getParentId, parentId);
        return BeanUtil.copyToList(this.list(wrapper), MenuDto.class);
    }
}
